﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace Anno.Util
{
    public static class AnnoProcess
	{
		/// <summary>
		/// 
		/// </summary>
		/// <param name="baseWorking"></param>
		/// <returns></returns>
		public static List<AnnoProcessInfo> GetAnnoProcesses(string baseWorking)
		{
			List<AnnoProcessInfo> list = new List<AnnoProcessInfo>();
			string text = Path.Combine(baseWorking, "proc");
			if (Directory.Exists(text))
			{
				string[] files = Directory.GetFiles(text);
				for (int i = 0; i < files.Length; i++)
				{
					AnnoProcessInfo annoProcesseByWorkingDirectory = AnnoProcess.GetAnnoProcesseByWorkingDirectory(new FileInfo(files[i]).Name, baseWorking);
					if (text != null)
					{
						list.Add(annoProcesseByWorkingDirectory);
					}
				}
			}
			return list;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="workingDirectory"></param>
		/// <param name="baseWorking"></param>
		/// <returns></returns>
		public static AnnoProcessInfo GetAnnoProcesseByWorkingDirectory(string workingDirectory, string baseWorking)
		{
			AnnoProcessInfo annoProcessInfo = null;
			object locker = AnnoProcess._locker;
			lock (locker)
			{
				try
				{
					using (FileStream fileStream = new FileStream(Path.Combine(baseWorking, "proc", workingDirectory), FileMode.Open))
					{
						using (StreamReader streamReader = new StreamReader(fileStream))
						{
							annoProcessInfo = JsonHelper.DeserializeObjectByJson<AnnoProcessInfo>(streamReader.ReadToEnd());
						}
					}
				}
				catch
				{
				}
			}
			try
			{
				if (annoProcessInfo != null)
				{
					if (annoProcessInfo.Id > 0)
					{
						Process processById = Process.GetProcessById(annoProcessInfo.Id);
						if (processById == null || processById.HasExited)
						{
							annoProcessInfo.Running = false;
						}
					}
					else if (annoProcessInfo.Id <= 0)
					{
						annoProcessInfo.Running = false;
					}
				}
			}
			catch
			{
				if (annoProcessInfo != null)
				{
					annoProcessInfo.Running = false;
				}
			}
			return annoProcessInfo;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="workingDirectory"></param>
		/// <param name="baseWorking"></param>
		/// <returns></returns>
		public static bool RemoveAnnoProcesseByWorkingDirectory(string workingDirectory, string baseWorking)
		{
			object locker = AnnoProcess._locker;
			lock (locker)
			{
				new FileInfo(Path.Combine(baseWorking, "proc", workingDirectory))
				{
					Attributes = FileAttributes.Normal
				}.Delete();
			}
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="annoProcess"></param>
		/// <param name="baseWorking"></param>
		/// <returns></returns>
		public static bool WriteAnnoProcesseByWorkingDirectory(AnnoProcessInfo annoProcess, string baseWorking)
		{
			string path = Path.Combine(baseWorking, "proc", annoProcess.WorkingDirectory);
			object locker = AnnoProcess._locker;
			lock (locker)
			{
				if (!Directory.Exists(Path.Combine(baseWorking, "proc")))
				{
					Directory.CreateDirectory(Path.Combine(baseWorking, "proc"));
				}
				using (FileStream fileStream = new FileStream(path, FileMode.Create))
				{
					using (StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.UTF8))
					{
						streamWriter.Write(JsonHelper.SerializeObjectToJson(annoProcess));
						streamWriter.Flush();
					}
				}
			}
			return true;
		}

		// Token: 0x04000001 RID: 1
		private static object _locker = new object();
	}
}
